package com.kaliterre.kalidroid.dal;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * @author FIL A2 - Alyssa Boucher, Maxime Bureau, Yohan Mercier, Nicolas Mortier, Zakaria Oualkadi.
 *
 */
public class MyDBHelper extends SQLiteOpenHelper {

	private static final String TABLE_BATTERYLOG = "BatteryLog";
	private static final String TABLE_SETTINGS = "Settings";
	
	private static final String COL_ID = "ID";
	private static final String COL_EVENT = "EVENT";
	private static final String COL_DATE = "DATE";
	private static final String COL_LEVEL = "LEVEL";
	private static final String COL_VOLTS = "VOLTS";
	private static final String COL_TEMPERATURE = "TEMPERATURE";
	private static final String COL_STATUS = "STATUS";
	
	private static final String COL_SONDE = "SONDE";
	private static final String COL_ACTIVATED = "ACTIVATED";
	
	private static final String CREATE_LOG_TABLE = "CREATE TABLE " + TABLE_BATTERYLOG + " ("
								+ COL_ID 			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
								+ COL_DATE  		+ " TEXT NOT NULL, "
								+ COL_EVENT 		+ " TEXT NOT NULL, "
								+ COL_LEVEL 		+ " TEXT NOT NULL, "
								+ COL_VOLTS 		+ " TEXT NOT NULL, " 
								+ COL_TEMPERATURE 	+ " TEXT NOT NULL, " 
								+ COL_STATUS 		+ " TEXT NOT NULL);";
	
	private static final String CREATE_SETTINGS_TABLE = "CREATE TABLE " + TABLE_SETTINGS + " ("
			+ COL_ID 			+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
			+ COL_SONDE  		+ " TEXT NOT NULL, "
			+ COL_ACTIVATED 		+ " INTEGER NOT NULL);";
	public MyDBHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}
 
	/* (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		db.execSQL(CREATE_LOG_TABLE);
		db.execSQL(CREATE_SETTINGS_TABLE);
		db.execSQL("INSERT INTO " + TABLE_SETTINGS + " (ID, SONDE, ACTIVATED) VALUES (0, 'Battery', 1);");
		db.execSQL("INSERT INTO " + TABLE_SETTINGS + " (ID, SONDE, ACTIVATED) VALUES (1, 'Call', 1);");
	}
 
	/* (non-Javadoc)
	 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Here we can do whatever we want. 
		// We choose to remove Tables and recrate its.
		db.execSQL("DROP TABLE " + TABLE_BATTERYLOG + ";");
		onCreate(db);
	}
}
